console.log(123);
jQuery(document).ready(function($) {
    const generatorForm = $('.deepseek_generator_form');
    const previewBox = $('.preview-box');
    const loading = $('.loading-indicator');
    console.log(generatorForm);
 
    // 生成内容
    
    $('#generate_btn').on('click', function(e) {

        e.preventDefault();
        
       const formData = {
        prompt: $('#deepseek_prompt').val(), // 修正为正确字段名
        content_type: $('#deepseek_content_type').val(),
        word_count: $('#deepseek_word_count').val(),
        nonce: deepseekVars.nonce
    };
        loading.show();
        
     // 修改后的generator.js
        $.post(deepseekVars.ajaxurl, {
            action: 'deepseek_generate',
            prompt: formData.prompt, // 使用修正后的字段名
            content_type: formData.content_type,
            word_count: formData.word_count,
            nonce: formData.nonce
        }, function(response) {
            loading.hide();
            if (response.success) {
                console.log('API Response:', response); // 添加调试日志
                previewBox.html(response.data.html);
                previewBox.data('raw', response.data.content);
                $('.button-group').show();
            } else {
                console.error('Error:', response);
                alert('生成失败：' + response.data);
            }
        }).fail(function(jqXHR, textStatus, errorThrown) {
            loading.hide();
            console.error('AJAX Error:', textStatus, errorThrown);
            alert('请求失败: ' + textStatus);
        });
    });

    // 确认内容
    $('.button-group').on('click', '#confirm_btn', function() {
        const content = previewBox.data('raw');
        
        $.post(deepseekVars.ajaxurl, {
            action: 'deepseek_confirm',
            content: content,
            nonce: deepseekVars.nonce
        }, function(response) {
            if (response.success) {
                
                $('#title').val(response.data.metadata.title);
                $('#zrz_seo_title').val(response.data.metadata.title);
    
                $('#new-tag-post_tag').val(response.data.metadata.keywords.join(','));
                $('#zrz_seo_keywords').val(response.data.metadata.keywords.join(',')); 
                
                $('#excerpt').val(response.data.metadata.description);
                $('#zrz_seo_description').val(response.data.metadata.description);
                
                tinymce.activeEditor.setContent(response.data.html_content);
                
                previewBox.empty();
                $('.button-group').hide();
            }
        });
    });
});